package ms.unicorn.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import ms.unicorn.dsl.HintedSpecializationType;
import ms.unicorn.dsl.ILanguageDiagram;
import ms.unicorn.dsl.ILanguageElement;
import ms.unicorn.dsl.IModelLanguage;
import ms.unicorn.dsl.LanguageRegistry;
import ms.unicorn.palette.LinkToolEntry;
import ms.unicorn.palette.NodeToolEntry;

import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.resource.ImageDescriptor;

/**
 * @generated NOT
 */
public class UnicornPaletteFactory {

	/**
	 * @param diagramEditor 
	 * @generated NOT
	 */
	public void fillPalette(PaletteRoot paletteRoot,
			UnicornDiagramEditor diagramEditor) {
		Diagram diagram = diagramEditor.getDiagram();
		ms.unicorn.Diagram domainDiagram = (ms.unicorn.Diagram) diagram
				.getElement();

		// check to filter tool  by domain language		

		List<IModelLanguage> languages = LanguageRegistry.INSTANCE
				.getLanguages(true);

		for (IModelLanguage lang : languages) {
			if (domainDiagram.getLanguage().equals(lang.getName())) {
				ILanguageDiagram diagramType = lang.getDiagram(domainDiagram
						.getElementType());
				Collection<ILanguageDiagram.IPalette> palettes = diagramType
						.getPalettes();

				for (ILanguageDiagram.IPalette palette : palettes) {
					PaletteDrawer container = new PaletteDrawer(
							palette.getText());
					container.setId(palette.getName());
					for (ILanguageElement element : palette.getElements()) {
						if (element != null) {
							ImageDescriptor smallImage = element
									.getSmallImage();
							ImageDescriptor largelImage = element
									.getLargeImage();
							String hint = null;
							ToolEntry entry = null;
							switch (element.getObjectType()) {
							case ILanguageElement.LANGUAGE_ENTITY:
								entry = new NodeToolEntry(element.getText(),
										element.getTooltip(), smallImage,
										largelImage, hint,
										Collections.singletonList(element
												.getIElementType()));
								break;
							case ILanguageElement.LANGUAGE_RELATION:
								entry = new LinkToolEntry(element.getText(),
										element.getTooltip(), smallImage,
										largelImage, hint,
										Collections.singletonList(element
												.getIElementType()));
								break;
							}
							if (element.getIElementType() instanceof HintedSpecializationType) {
								HintedSpecializationType eType = (HintedSpecializationType) element
										.getIElementType();
								eType.setLanguage(lang);
								eType.setSemanticHint(hint);
							}
							if (entry != null) {
								entry.setToolProperty("language", lang);
								entry.setToolProperty("typeId",
										element.getName());
								entry.setToolProperty("semanticHint", hint);
								container.add(entry);
							}
						}
					}
					paletteRoot.add(container);
				}
			}
		}
	}

}
